import { createRouter, createWebHistory } from "vue-router";
import { showToast } from 'vant';

import ChartPage from "@/views/Chart/ChartPage.vue";
import AssetsPage from "@/views/Assets/AssetsPage.vue";
import AssetTypeSelect from "@/views/Assets/AssetTypeSelect.vue";
import AssetDetailInput from "@/views/Assets/AssetDetailInput.vue";
import AssetEditPage from "@/views/Assets/AssetEditPage.vue";
import { getToken } from "@/utils/token";
import Login from "@/views/login/Login.vue";
import Register from "@/views/login/Register.vue";
import FindPassword from "@/views/login/FindPassword.vue";
import Detail from "@/views/detail/Detail.vue";
import Mine from "@/views/mine/Mine.vue";
import TallyForm from "@/views/tally/TallyForm.vue";
import CategorySettings from "@/views/tally/CategorySettings.vue";
import HomePage from "@/views/login/HomePage.vue";
import MoblieLogin from "@/views/login/moblieLogin.vue";
import Feedback from "@/views/mine/Feedback.vue";
import UseHelp from "@/views/mine/UseHelp.vue";
import MineDetail from "@/views/mine/MineDetail.vue";
import Revoked from "@/views/login/Revoked.vue";
import MineInformation from "@/views/mine/MineInformation.vue";
import AboutSharkBookkeeping from "@/views/mine/AboutSharkBookkeeping.vue";
import AccountOverview from "@/views/mine/AccountOverview.vue";
import Ledger from "@/views/mine/Ledger.vue";
import LedgerDetails from "@/views/mine/LedgerDetails.vue";
import Message from "@/views/mine/Message.vue";


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      {
      path: "/",
      redirect: "/home", // 修改默认页面为明细页
    },
    {
      name: "mobileLogin",
      path: "/mobileLogin",
      component: MoblieLogin,
      meta: { requiresAuth: false } // 不需要登录
    },
    {
      name: "home",
      path: "/home",
      component: HomePage,
      meta: { requiresAuth: false } // 不需要登录
    },
    {
      name: "login",
      path: "/login",
      component: Login,
      meta: { requiresAuth: false } // 不需要登录
    },
    
    {
      name: "register",
      path: "/register",
      component: Register,
      meta: { requiresAuth: false } // 不需要登录
    },
    {
      name: "findPassword",
      path: "/findPassword",
      component: FindPassword,
      meta: { requiresAuth: false } // 不需要登录
    },
    {
      name: "detail",
      path: "/detail",
      component: Detail,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "asset", // 资产页面，对应Bottom2中的asset
      path: "/asset",
      component: AssetsPage,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "assets", // 保留原有路由作为别名
      path: "/assets",
      redirect: "/asset"
    },
    {
      name: "tallyForm",
      path: "/tallyForm",
      component: TallyForm,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "categorySettings",
      path: "/category-settings",
      component: CategorySettings,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "chart",
      path: "/chart",
      component: ChartPage,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "mine",
      path: "/mine",
      component: Mine,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "revoked",
      path: "/revoked",
      component: Revoked,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "feedback",
      path: "/feedback",
      component: Feedback,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "useHelp",
      path: "/use-help",
      component: UseHelp,
      meta: { requiresAuth: false } // 不需要登录
    },
    {
      name: "mineDetail",
      path: "/mine-detail",
      component: MineDetail,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "mineInformation",
      path: "/mine-information",
      component: MineInformation,
      meta: { requiresAuth: true } // 需要登录
    },
{
      name: "aboutSharkBookkeeping",
      path: "/about-shark-bookkeeping",
      component: AboutSharkBookkeeping,
      meta: { requiresAuth: true } // 不需要登录
    },
    {
      name: "accountOverview",
      path: "/account-overview",
      component: AccountOverview,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "ledger",
      path: "/ledger",
      component: Ledger,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "ledgerDetails",
      path: "/ledger-details",
      component: LedgerDetails,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "message",
      path: "/message",
      component: Message,
      meta: { requiresAuth: true } // 需要登录
    },

    {
      name: "assetTypeSelect",
      path: "/asset-type-select",
      component: AssetTypeSelect,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "assetDetailInput",
      path: "/asset-detail-input",
      component: AssetDetailInput,
      meta: { requiresAuth: true } // 需要登录
    },
    {
      name: "assetEdit",
      path: "/asset-edit",
      component: AssetEditPage,
      meta: { requiresAuth: true } // 需要登录
    },
  ],
});

// 不需要登录就可以访问的页面白名单
const whiteList = ['/home', '/login', '/mobileLogin', '/register', '/findPassword'];

// 登录相关页面列表（已登录用户访问这些页面会重定向到明细页）
const authPages = ['/home', '/login', '/mobileLogin', '/register', '/findPassword'];

// 全局前置路由守卫
router.beforeEach((to, from, next) => {
  const token = getToken();
  
  // 如果用户已登录
  if (token) {
    // 如果要访问登录相关页面，重定向到明细页
    if (authPages.includes(to.path)) {
      showToast('您已登录');
      next({ path: '/detail' });
    } else {
      // 其他页面正常访问
      next();
    }
  } else {
    // 用户未登录
    // 如果要访问的页面在白名单中，允许访问
    if (whiteList.includes(to.path)) {
      next();
    } else {
      // 如果要访问的页面需要登录，重定向到首页
      showToast('请先登录');
      next({
        path: '/home',
        query: { redirect: to.fullPath } // 保存用户想访问的页面，登录后跳转
      });
    }
  }
});

export default router;

